//
//  ResidentManagementViewController.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/7/3.
//

import UIKit
import SnapKit
import CollectionKit

/// 居民管理
class ResidentManagementViewController: UIViewController {
  var collectionView = CollectionView()

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    opacityNavigationBar(color: .designKit.background)
  }

  override func viewDidLoad() {
    super.viewDidLoad()

    title = "居民管理"
    view.backgroundColor = .designKit.secondaryBackground

    let searchView = R.nib.searchContentView(owner: self)!
    searchView.filterButton.isHidden = true
    searchView.textField.placeholder = "搜索产权人姓名"
    view.addSubview(searchView)

    let filterView = MultifilterContentView<ResidentFilter>(frame: .zero)
    filterView.datasource = [
      ResidentFilter(text: "楼栋"),
      ResidentFilter(text: "单元"),
      ResidentFilter(text: "房间号"),
    ]
    view.addSubview(filterView)

    view.addSubview(collectionView)

    searchView.snp.makeConstraints { make in
      make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
      make.left.right.equalToSuperview()
      make.height.equalTo(60)
    }

    filterView.snp.makeConstraints { make in
      make.left.right.equalToSuperview()
      make.top.equalTo(searchView.snp.bottom)
      make.height.equalTo(44)
    }

    collectionView.snp.makeConstraints { make in
      make.top.equalTo(filterView.snp.bottom)
      make.left.right.bottom.equalToSuperview()
    }

    let dataSource = ArrayDataSource<ResidentManagementModel>(
      data: ResidentManagementModel.items
    )

    let viewSource = ClosureViewSource<ResidentManagementModel, ResidentManagementItemView> { data, index in
      return R.nib.residentManagementItemView(owner: self)!
    } viewUpdater: { item, data, index in
      item.model = data
    }

    let sizeSource = { (index: Int, data: ResidentManagementModel, size: CGSize) in
      return CGSize(width: size.width, height: 62)
    }

    let itemProvider = BasicProvider<ResidentManagementModel, ResidentManagementItemView>(
      dataSource: dataSource,
      viewSource: viewSource,
      sizeSource: sizeSource,
      layout: FlowLayout(spacing: 8, justifyContent: .center)
        .inset(by: .padding(15, 8))
    )

    itemProvider.animator = FadeAnimator()
    itemProvider.tapHandler = { (context) in

    }

    collectionView.provider = ComposedProvider(sections: [itemProvider])
  }


  /*
   // MARK: - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // Get the new view controller using segue.destination.
   // Pass the selected object to the new view controller.
   }
   */

}

struct ResidentFilter: MultifilterContent {
  let text: String
}


struct ResidentManagementModel {
  let unit: String
  let name: String
  let phone: String
}

extension ResidentManagementModel {
  static let items: [ResidentManagementModel] = (1...6).map {
    .init(unit: "1栋1单元00\($0 + 1)", name: "詹老板\($0 + 1)", phone: "1750092837\($0)")
  }
}
